package com.example.questionnaire.controller;


import com.example.questionnaire.common.ServerResponse;
import com.example.questionnaire.exception.RException;
import com.example.questionnaire.pojo.DO.EvaluateQuestion;
import com.example.questionnaire.pojo.DO.ScoreQuestion;
import com.example.questionnaire.pojo.VO.EvaluateQuestionVO;
import com.example.questionnaire.pojo.VO.ScoreQuestionVO;
import com.example.questionnaire.service.AppidAndSecretService;
import com.example.questionnaire.service.RatingQuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;


@RestController
@RequestMapping("/RatingQuestion")
@Api(tags = "评分和评价")
public class RatingQuestionController {

    @Autowired
    private RatingQuestionService ratingQuestionService;

    @Autowired
    AppidAndSecretService appidAndSecretService;


    @PostMapping("/updateAddScoreQuestion")
    @ApiOperation(value = "创建或修改评分题", response = ServerResponse.class, httpMethod = "POST")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse updateAddScoreQuestion(@RequestParam String formid,
                                                 @RequestParam String subjectId,
                                                 @RequestParam String title,
                                                 @RequestParam String scoreSettings,
                                                 @RequestParam boolean isMustAnswer,
                                                 @RequestParam String questionsJumpItems,
                                                 @RequestParam String questionsJumpArray,
                                                 @RequestParam int questionType,
                                                 @RequestParam String jumpingType) throws IOException {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        if(appidAndSecretService.tokenTimeout(token)){
            String msg = "登录已过期";
            return ServerResponse.createBySuccess(10010,msg, null);

        }
        if (ratingQuestionService.updateAddScoreQuestion(formid,
               subjectId,
                title,
                scoreSettings,
                isMustAnswer,
                questionsJumpItems,
                questionsJumpArray,
                questionType,
                jumpingType)){
            return ServerResponse.createBySuccess("添加成功");
        }
        return ServerResponse.createBySuccess("修改成功");

    }

    @PostMapping("/updateAddEvaluateQuestion")
    @ApiOperation(value = "创建或修改评价题", response = ServerResponse.class, httpMethod = "POST")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse updateAddEvaluateQuestion(@RequestParam String formid,
                                                    @RequestParam String subjectId,
                                                    @RequestParam String title,
                                                    @RequestParam String evaluation,
                                                    @RequestParam boolean isMustAnswer,
                                                    @RequestParam String questionsJumpItems,
                                                    @RequestParam String questionsJumpArray,
                                                    @RequestParam int questionType,
                                                    @RequestParam String jumpingType) throws IOException {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        if(appidAndSecretService.tokenTimeout(token)){
            String msg = "登录已过期";
            return ServerResponse.createBySuccess(10010,msg, null);

        }
        if (ratingQuestionService.updateAddEvaluateQuestion(formid,
                subjectId,
                title,
                evaluation,
                isMustAnswer,
                questionsJumpItems,
                questionsJumpArray,
                questionType,
                jumpingType)){
            return ServerResponse.createBySuccess("添加成功");
        }
        return ServerResponse.createBySuccess("修改成功");

    }

}
